package com.woniu.venue.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.exception.recruit.RecruitException;
import com.woniu.exception.recruit.RecruitExceptionCode;
import com.woniu.venue.dto.recruit.RecruitDto;
import com.woniu.venue.model.Recruit;
import com.woniu.venue.dao.RecruitDao;
import com.woniu.venue.param.recruit.*;
import com.woniu.venue.service.RecruitService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年07月12日
 */
@Service
public class RecruitServiceImpl extends ServiceImpl<RecruitDao, Recruit> implements RecruitService {

    @Autowired
    private RecruitDao recruitDao;

    @Override
    public RecruitDto addRecruit(AddRecruitParam param) {
        Recruit recruit = BeanUtil.toBean(param, Recruit.class);
        // 获取当前时间
        recruit.setRecruitPublishtime(DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss"));
        recruitDao.insert(recruit);
        RecruitDto recruitDto = BeanUtil.toBean(recruit, RecruitDto.class);
        return recruitDto;
    }

    @Override
    public void deleteRecruit(Integer recruitId) {
        recruitDao.deleteById(recruitId);
    }

    @Override
    public PageInfo<RecruitDto> searchRecruit(SearchRecruitParam param) {
        PageHelper.startPage(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 6 : param.getPageSize());
        QueryWrapper<Recruit> qw = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(param.getContent()))
            qw.like("recruit_title", param.getContent()).or().like("recruit_message", param.getContent());
        if (ObjectUtil.isNotEmpty(param.getStartTime()))
            qw.ge("recruit_publishtime", param.getStartTime());
        if (ObjectUtil.isNotEmpty(param.getEndTime()))
            qw.le("recruit_publishtime", param.getEndTime());
//        qw.orderBy(true, param.getOrder() == null || param.getOrder() == 1 ? true : false, "recruit_publishtime");
        List<Recruit> recruits = recruitDao.selectList(qw);
        List<RecruitDto> recruitDtos = BeanUtil.copyToList(recruits, RecruitDto.class);
        PageInfo<RecruitDto> recruitDtoPageInfo = new PageInfo<>(recruitDtos);
        return recruitDtoPageInfo;
    }

    @Override
    public PageInfo<RecruitDto> loadRecruit(LoadRecruitParam param) {
        PageHelper.startPage(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 6 : param.getPageSize());
        List<RecruitDto> recruitDtos = recruitDao.load(param);
        PageInfo<RecruitDto> recruitDtoPageInfo = new PageInfo<>(recruitDtos);
        return recruitDtoPageInfo;
    }


    @Override
    public PageInfo<RecruitDto> loadForCoach(LoadRecruitForCoachParam param) {
        PageHelper.startPage(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 6 : param.getPageSize());
        QueryWrapper<Recruit> qw = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(param.getContent()))
            qw.like("recruit_title", param.getContent()).or().like("recruit_message", param.getContent());
        qw.orderBy(true, param.getOrder() == null || param.getOrder() == 1 ? true : false, "recruit_publishtime");
        List<Recruit> recruits = recruitDao.selectList(qw);
        List<RecruitDto> recruitDtos = BeanUtil.copyToList(recruits, RecruitDto.class);
        PageInfo<RecruitDto> recruitDtoPageInfo = new PageInfo<>(recruitDtos);
        return recruitDtoPageInfo;
    }

    @Override
    public RecruitDto showRecruit(Integer id) {
        Recruit recruit = recruitDao.selectById(id);
        RecruitDto recruitDto = BeanUtil.toBean(recruit, RecruitDto.class);
        return recruitDto;
    }

    @Override
    public RecruitDto updateRecruit(UpdateRecruitParam param) {
        Recruit recruit = recruitDao.selectById(param.getRecruitId());
        if (ObjectUtil.isEmpty(recruit))
            throw new RecruitException(RecruitExceptionCode.RECRUIT_NOT_EXIST);
        if (ObjectUtil.isNotEmpty(param.getRecruitImg()))
            recruit.setRecruitImg(param.getRecruitImg());
        if (ObjectUtil.isNotEmpty(param.getRecruitMessage()))
            recruit.setRecruitMessage(param.getRecruitMessage());
        if (ObjectUtil.isNotEmpty(param.getRecruitSalary()))
            recruit.setRecruitSalary(param.getRecruitSalary());
        if (ObjectUtil.isNotEmpty(param.getRecruitTitle()))
            recruit.setRecruitTitle(param.getRecruitTitle());
        recruitDao.updateById(recruit);
        RecruitDto recruitDto = BeanUtil.toBean(recruit, RecruitDto.class);
        return recruitDto;
    }
}
